//
//  KSPickerPhotoBottomView.swift
//  Supermarket
//
//  Created by Black_Crystal on 2017/9/20.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

protocol CompleteDelegate: NSObjectProtocol {
    func completeCallBack()
}

class KSPickerPhotoBottomView: UIView {
    
    weak var delegaet: CompleteDelegate?
    //已选照片数量标签
    var numLabel:UILabel!
    //按钮标题标签“完成”
    var completeBtn: UIButton!
    
    //按钮的默认尺寸
    private let defaultFrame = CGRect(x:0, y:0, width:70, height:20)
    
    //文字颜色（同时也是数字背景颜色）
    private let titleColor = UIColor(red: 0x09/255, green: 0xbb/255, blue: 0x07/255, alpha: 1)
    
    override init(frame: CGRect) {
        
        super.init(frame:frame)
        
        //已选照片数量标签初始化
        numLabel = UILabel()
        numLabel.backgroundColor = titleColor
        numLabel.layer.cornerRadius = 10
        numLabel.layer.masksToBounds = true
        numLabel.textAlignment = .center
        numLabel.font = UIFont.systemFont(ofSize: 15)
        numLabel.textColor = UIColor.white
        numLabel.text = "9"
        numLabel.isHidden = true
        self.addSubview(numLabel)
        
        //按钮标题标签初始化
        self.completeBtn = UIButton()
        completeBtn.addTarget(self, action: #selector(completeEvent), for: UIControlEvents.touchDown)
        completeBtn.setTitle("完成", for: UIControlState.normal)
        completeBtn.setTitleColor(titleColor, for: UIControlState.normal)
        self.addSubview(completeBtn)
        completeBtn.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
        }
        numLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalTo(completeBtn.snp.left).offset(-10)
            make.width.height.equalTo(20)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    //用户数字改变时播放的动画
    func playAnimate() {
        //从小变大，且有弹性效果
        self.numLabel.isHidden = false
        self.numLabel.transform = CGAffineTransform(scaleX: 0.1, y: 0.1)
        UIView.animate(withDuration: 0.5, delay: 0, usingSpringWithDamping: 0.5,
                       initialSpringVelocity: 0.5, options: UIViewAnimationOptions(),
                       animations: {
                        self.numLabel.transform = CGAffineTransform.identity
        }, completion: nil)
    }
    
    @objc func completeEvent() {
        self.delegaet?.completeCallBack()
    }
}
